import { retCode, retMsg, retLevel } from './RetEnum.ts'

export const ErrorTypes = {
  SYSTEM_ERROR: {
    code: retCode.SYSTEM_ERROR,
    message: retMsg.SYSTEM_ERROR,
    logLevel: retLevel.ERROR,
  },
  SUCCESS: {
    code: retCode.SUCCESS,
    message: retMsg.SUCCESS,
    logLevel: retLevel.INFO,
  },
  FAIL: {
    code: retCode.FAIL,
    message: retMsg.FAIL,
    logLevel: retLevel.INFO,
  },
  NO_SUCH_PERMISSION: {
    code: retCode.NO_SUCH_PERMISSION,
    message: retMsg.NO_SUCH_PERMISSION,
    logLevel: retLevel.WARN,
  },
} as const

// 获取类型
export type ErrorType = keyof typeof ErrorTypes
export type ErrorInfo = (typeof ErrorTypes)[ErrorType]

//状态缓存
const CodeToMap = new Map<string, ErrorType>(
  (Object.entries(ErrorTypes) as [ErrorType, ErrorInfo][]).map(([type, info]) => [info.code, type]),
)

//根据状态码获取对应枚举
export function getEnumByCode(code: string): ErrorType | null {
  return CodeToMap.get(code) ?? null
}
